package com.yc.post.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.api.CommonPage;
import com.yc.api.CommonResult;
import com.yc.base.BaseController;
import com.yc.post.entity.Post;
import com.yc.post.service.IPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台岗位表 前端控制器
 * </p>
 *
 * @author bao
 * @since 2021-12-09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/post")
@Api(value = "PostController", tags = "后台岗位表")
public class PostController extends BaseController {

    private final IPostService postService;

    @PostMapping("/add")
    @ApiOperation(value = "创建Post", notes = "创建Post")
    @SaCheckPermission(value = {"management:post:add"})
    public CommonResult<?> create(@RequestBody Post post) {
        post.setId(null);
        boolean isOk = postService.create(post);
        if (isOk) {
            return CommonResult.success(post);
        }
        return CommonResult.failed();
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(value = "删除Post", notes = "删除Post")
    @SaCheckPermission(value = {"management:post:delete"})
    public CommonResult<?> deleteByIds(@RequestBody List<Long> ids) {
        boolean isOk = postService.delete(ids);
        if (isOk) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/updateById/{id}")
    @ApiOperation(value = "更新Post", notes = "更新Post")
    @SaCheckPermission(value = {"management:post:update"})
    public CommonResult<?> updateById(@PathVariable("id") long id, @RequestBody Post post) {
        post.setId(id);
        boolean isOk = postService.update(post);
        if (isOk) {
            return CommonResult.success(post);
        }
        return CommonResult.failed();
    }

    @PostMapping("/queryById/{id}")
    @ApiOperation(value = "根据Id获取Post", notes = "根据Id获取Post")
    public CommonResult<?> queryById(@PathVariable("id") Long id) {
        Post post = postService.queryById(id);
        if (post != null) {
            return CommonResult.success(post);
        }
        return CommonResult.failed();
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取Post列表", notes = "获取Post列表")
    public CommonResult<?> listAll() {
        List<Post> list = postService.selectList();
        return CommonResult.success(list);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取Post列表", notes = "获取Post列表")
    public CommonResult<?> pageAll(@RequestBody JSONObject params) {
        int pageNum = params.getInt("pageNum", 1);
        int pageSize = params.getInt("pageSize", 25);
        IPage<Post> page = postService.selectPage(new Page<>(pageNum, pageSize));
        return CommonResult.success(CommonPage.restPage(page));
    }

}
